﻿#pragma once
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsItem>
#include <QPainter>
#include <QPen>
#include <QGraphicsView>

using namespace std;
#include "math.h"


class GraphicsShapeRectangleCirce : public QObject,public QGraphicsItem
{
    Q_OBJECT
    Q_INTERFACES(QGraphicsItem)
public:

    QString m_message="";
    int m_shape_type =0;
    bool m_bool_filled = 0;

    bool m_move_enable=true;
    bool m_moving=false;
    QPoint  m_pressedPos ;
    QPoint  m_startPos ;

    vector<QPoint> m_points;

    QPen m_pen;
    int  m_point_half_size=10;
    bool  m_in = false;

    int m_editingVertex =-1;
    QPoint m_dragStartPosition;
    QColor m_color;

signals:
    void sendMessage(QPointF pos);
public:
    GraphicsShapeRectangleCirce(qreal x, qreal y, qreal width, qreal height,QColor color)
    {
        this->setAcceptHoverEvents(true);
        this->setFlags(QGraphicsItem::ItemIsSelectable |
                       QGraphicsItem::ItemIsFocusable);

        this->setCursor(Qt::PointingHandCursor);

        m_points.push_back(QPoint(x,y));
        m_points.push_back(QPoint(x+width,y+height));

        m_pen.setColor(color);
        m_pen.setWidth(8);
        m_pen.setStyle(Qt::SolidLine);
        m_color = color;
    }

    QRectF boundingRect() const override
    {

        return QRectF(m_points[0].x()-m_point_half_size,m_points[0].y()-m_point_half_size,m_points[1].x()-m_points[0].x()+m_point_half_size*2,m_points[1].y()-m_points[0].y()+m_point_half_size*2);
    }

    QRectF returnRect() const
    {
        return QRectF(m_points[0].x(),m_points[0].y(),m_points[1].x()-m_points[0].x(),m_points[1].y()-m_points[0].y());
    }

    //    QPainterPath  shape() const {
    //        QPainterPath   path;
    //        path.addEllipse(m_points[0].x(),m_points[0].y(),m_points[1].x()-m_points[0].x(),m_points[1].y()-m_points[0].y());
    //        return path;
    //    }

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
    {
        Q_UNUSED(option);
        Q_UNUSED(widget);
        painter->setPen(m_pen);
        QPainterPath   path;

        if(m_shape_type==0)
        {
            path.addRect(m_points[0].x(),m_points[0].y(),m_points[1].x()-m_points[0].x(),m_points[1].y()-m_points[0].y());
        }
        else if(m_shape_type==1)
        {
            path.addEllipse(m_points[0].x(),m_points[0].y(),m_points[1].x()-m_points[0].x(),m_points[1].y()-m_points[0].y());
        }

        if (m_bool_filled)
        {
            painter->fillPath(path, QBrush(m_color));
        }

        painter->drawPath(path);
          /*if(m_in)
          {*/
            QPainterPath   vertex_path;
            for(auto obj : m_points)
                    vertex_path.addRect(QRect(obj.x() - m_point_half_size, obj.y() - m_point_half_size, m_point_half_size*2, m_point_half_size*2));
            painter->drawPath(vertex_path);

            QFont font("Arial", 50); // 设置字体和大小
            painter->setPen(Qt::darkCyan);
            painter->setFont(font);
            painter->drawText(m_points[1].x()+m_point_half_size,m_points[1].y()+m_point_half_size, m_message);
          //}

    }

    void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override
    {
        m_in =true;
        QGraphicsItem::hoverEnterEvent(event);
    }

    void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override
    {
        m_in =false;
        QGraphicsItem::hoverLeaveEvent(event);
    }

    void hoverMoveEvent(QGraphicsSceneHoverEvent* event) override
    {
        QPointF pos = event->pos();
        emit sendMessage(pos);
    }

    void mousePressEvent(QGraphicsSceneMouseEvent *event) override {

        if(m_move_enable ==false)
        {
            return;
        }
        for (int i = 0; i < m_points.size(); ++i) {
            if (abs(m_points[i].x()-event->pos().x()<=m_point_half_size && abs(m_points[i].y()-event->pos().y())<=m_point_half_size))
            {
                m_editingVertex = i;
                m_dragStartPosition = m_points[i];
                update(); // 触发重绘
                return;
            }
        }
        m_moving = true;
        m_pressedPos  = event->scenePos().toPoint();
        m_startPos = this->pos().toPoint();
    }

    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override {
        if(m_move_enable ==false)
        {
            return;
        }
        if (m_editingVertex != -1) {
            QPoint pos = event->pos().toPoint();
            QPoint poss =  pos ;
            m_points[m_editingVertex] =poss;
            update(); // 触发重绘
        }
        else if(m_moving) {
            // 获取场景坐标和本地坐标
            QPoint pos = event->scenePos().toPoint();

            // 计算偏移
            qreal xInterval = pos.x() - m_pressedPos .x();
            qreal yInterval = pos.y() - m_pressedPos .y();

            // 设置在场景中位置
            this->setPos(m_startPos + QPointF(xInterval, yInterval));
            this->update();
        }
    }

    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override {
        Q_UNUSED(event);
        if(m_move_enable ==false)
        {
            return;
        }
        m_moving=false;
        if (m_editingVertex != -1) {
               m_editingVertex = -1; // 停止编辑
               update();
        }
    }

    void setShapeType(int value)  {
        m_shape_type = value;
        update();
    }

    void setFilled(bool value) {
        m_bool_filled = value;
        update();
    }
};

